<template>
	<view class="">
		<view class="top">
			<view class="canpin" v-for="(item,index) in cartList" :key="index">
				<view class="">
					<image style="width: 100rpx;height: 100rpx;" :src="item.good.images" mode=""></image>
				</view>
				<view class="wenzi">
					<view class="">
						{{item.good.title}}
					</view>
					<view class="">
						x{{item.number}}
					</view>
				</view>
				<view class="">
					￥{{item.good.discount_price}}
				</view>
			</view>
			
			
			
		</view>
		
		<view class="write">
			<view class="" style="display: flex;justify-content: space-between;height: 70rpx;align-items: center;padding: 10rpx;border-bottom: 1rpx solid #ccc;">
				<view class="">
					请输入备注
				</view>
				<view class="">
					<input style="" type="text" placeholder="请输入备注" />
				</view>
			</view>
			<view class="" style="display: flex;justify-content: space-between;height: 70rpx;align-items: center;padding: 10rpx;border-bottom: 1rpx solid #ccc;">
				<view class="">
					请选择地址
				</view>
				<view class="" style="display: flex;color: #ccc;">
					<view class="">
						默认地址为收货地址
					</view>
					<u-icon @click="goMyAddress" name="arrow-right" color="#000" size="28"></u-icon>
				
				</view>
			</view>
			<view @click="goYhq" class="" style="display: flex;justify-content: space-between;height: 70rpx;align-items: center;padding: 10rpx;border-bottom: 1rpx solid #ccc;">
				<view class="">
					选择优惠券
				</view>
				<view class="">
					<!-- <input style="" type="text" placeholder="请输入备注" /> -->
					<u-icon name="arrow-right" color="#000" size="28"></u-icon>
				</view>
			</view>
			
		</view>
		
		
		
		
		<view class="" style="padding-right: 20rpx;width: 250rpx;margin-left: 450rpx;margin-top: 20rpx;text-align: center;">
			总价格 ：{{totalPrice}}
		</view>
		
		<view @click="pay" class="" style="width: 200rpx;background-color: red;height: 80rpx;line-height: 80rpx;text-align: center;color: #fff;border-radius: 20rpx;margin: 0 auto;margin-top: 30rpx;">
			结算
		</view>
		
		
	</view>
	
</template>

<script>
	export default{
		onShow(){
			this.getCartList()
			this.user_coupon_id = uni.getStorageSync('youhuiquanId')
		},
		data(){
			return {
				cartList:[],
				totalNumber:'',
				totalPrice:'',
				cartsShop:[],
				user_coupon_id:'',
				// 货物id和数量
				goodsIds: [],
				goodsNums: []
			}
			
		},
		methods:{
			getCartList(){
				this.$req('POST',this.$api.cartList,{
					user_id:uni.getStorageSync('userId')
				}).then(res =>{
					
					this.cartList = res.data.list
					
					this.totalNumber = this.cartList.reduce((sum, item) => sum + item.number, 0);
					
					// this.totalPrice = this.cartList.reduce((sum, item) => sum + item.number * parseFloat(item.good.discount_price), 0);
					this.totalPrice = this.cartList.reduce((sum, item) => sum + item.number * parseFloat(item.good.discount_price), 0);
					this.totalPrice = this.totalPrice.toFixed(2);
					
					this.goodsIds = [];
					this.goodsNums = [];
					this.cartList.forEach(item => {
						this.goodsIds.push(item.goods_id);
						this.goodsNums.push(item.number)
					});
					console.log(this.cartList,'遍历之前')
					console.log(this.cartsShop,'遍历之后')
					
					
					
				})
			},
			goMyAddress(){
				uni.navigateTo({
					url:'/myaddress-pkg/myaddress/myaddress'
				})
			},
			goYhq(){
				uni.navigateTo({
					url:`/youhuiquan-pkg/youhuiquan/youhuiquan?type=${1}&money=${this.totalPrice}`
				})
			},
			pay(){
				if(this.user_coupon_id == ''){
					this.$req('POST',this.$api.peyMoney,{
						user_id:uni.getStorageSync('userId'),
						school_id:uni.getStorageSync('addressId'),
						// cart: JSON.stringify(this.cartsShop),
						goods_id: this.goodsIds.join(','),
						number: this.goodsNums.join(','),
						pay_type:1,
						
					}).then(res =>{
						if(res.status == 200){
							uni.requestPayment({
								provider: 'wxpay',
								timeStamp: res.data.timeStamp,
								nonceStr: res.data.nonceStr,
								package: res.data.package,
								signType: res.data.signType,
								paySign: res.data.paySign,
								success: payRes => {
									uni.$u.toast('支付成功')
									 setTimeout(() =>{
										 uni.redirectTo({
										      url: '/pages/index/index',  // 这里的路径根据你的实际项目情况调整
										    });
									 },1000)
									 // this.getinfo()
									 
								},
								fail: err => {
									uni.$u.toast('支付失败')
								}
							});
						}
					})
				}else{
					this.$req('POST',this.$api.peyMoney,{
						user_id:uni.getStorageSync('userId'),
						school_id:uni.getStorageSync('addressId'),
						cart:this.cartsShop,
						pay_type:1,
						user_coupon_id:this.user_coupon_id
					}).then(res =>{
						if(res.status == 200){
							uni.requestPayment({
								provider: 'wxpay',
								timeStamp: res.data.timeStamp,
								nonceStr: res.data.nonceStr,
								package: res.data.package,
								signType: res.data.signType,
								paySign: res.data.paySign,
								success: payRes => {
									uni.$u.toast('支付成功')
									 setTimeout(() =>{
										 uni.redirectTo({
										      url: '/pages/index/index',  // 这里的路径根据你的实际项目情况调整
										    });
									 },1000)
									 // this.getinfo()
									 
								},
								fail: err => {
									uni.$u.toast('支付失败')
								}
							});
						}
						
					})
				}
				
				
				
				
				
			}
			
			
			
		}
		
	}
</script>

<style>
	page {
		background: #f6f5f7;
	}
	
	.top{
		width: 95%;
		background-color: #fff;
		margin: 30rpx auto;
	}
	
	.canpin{
		display: flex;
		justify-content: space-between;
		text-align: center;
		align-items: center;
		padding: 20rpx;
		border-radius: 20rpx;
	}
	
	.wenzi{
		/* display: flex; */
	}
	
	.jiesuan{
		width: 200rpx;
		margin: 0 auto;
		margin-top: 200rpx;
		text-align: center;
		height: 80rpx;
		line-height: 80rpx;
		color: #fff;
		background-color:bisque;
		border-radius: 10rpx;
	}
	
	.write{
		width: 95%;
		background-color: #fff;
		border-radius: 20rpx;
		margin: 0 auto;
	}
	
	
	
	
</style>